from flask import Blueprint, request, session
from flask_restful import Api, Resource, marshal_with
from flask_restful.reqparse import RequestParser

from utils.render import json_detail_render
from webapp.config import api_utils
from webapp.config.api_utils import oper_log, check_auth
from webapp.dto_mapper import course_page_fields, course_table_fields, score_page_fields, score_fields
from webapp.schema import GradeSchema, CourseSchema, ScoreSchema
from webapp.service.course_service import CourseService
from webapp.service.grade_service import GradeService
from webapp.service.score_service import ScoreService

wx_score_app = Blueprint('wxscore', __name__, url_prefix='/wxapi/score')
wx_score_api = Api(wx_score_app)

parser = RequestParser()
parser.add_argument('id', location='json', type=int, trim=True, required=True)
parser.add_argument('uId', location='json', type=int, trim=True, required=False)
parser.add_argument('sessionId', location='json', type=int, trim=True, required=False)
parser.add_argument('courseId', location='json', type=int, trim=True, required=False)
parser.add_argument('resourceId', location='json', type=int, trim=True, required=False)
parser.add_argument('teachtype', location='json', type=int, trim=True, required=False)
parser.add_argument('score', location='json', type=str, trim=True, required=False)



@wx_score_api.resource("", "/")
class ScoreWxApi(Resource):
    @marshal_with(score_page_fields)
    def get(self):
        params = request.args.items(multi=True)
        params_dict = api_utils.build_params(params)
        return ScoreService.query_uId_SessionId(**params_dict)


@wx_score_api.resource("", "/<int:id>")
class FindScoreByIdApi(Resource):
    @marshal_with(score_fields)
    def post(self, id):
        result = ScoreService.find_detail_score_by_id(id)
        return result